//
//  SRAccountService.m
//  SportRing
//
//  Created by mykoma on 14/10/26.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRAccountNetService.h"
#import "SRAccountNetSource.h"
#import "SRDB.h"

@implementation SRAccountNetService

//登录 2
+ (RACSignal *)loginWithUserName:(NSString *)userName userPwd:(NSString *)userPwd
{
    RACSubject * subject = [RACSubject subject];
    
    [SRAccountNetSource loginWithUserName:userName
                                  userPwd:[userPwd md5]
                                  success:^(NSDictionary *dict)
     {
         if (dict[@"code"] && [dict[@"code"] intValue] == 0)
         {
             SRMe *me = [SRMe findUserAndCreateIfNotExistByName:userName];
             [me configAttributesWithBlock:^(SRMe *meSelf) {
                 meSelf.nickName   = [NSString stringWithFormat:@"%@",dict[@"data"][@"nickName"]];
                 meSelf.height     = [NSNumber numberWithInteger:[dict[@"data"][@"height"] integerValue]];
                 meSelf.weight     = [NSNumber numberWithInteger:[dict[@"data"][@"weight"] integerValue]];
                 meSelf.gender     = [NSNumber numberWithInteger:[dict[@"data"][@"gender"] integerValue]];
                 meSelf.createDate = [NSString stringWithFormat:@"%@",dict[@"data"][@"createDate"]];
                 meSelf.birthday   = [NSString stringWithFormat:@"%@",dict[@"data"][@"birthDate"]];
                 meSelf.token      = [NSString stringWithFormat:@"%@",dict[@"data"][@"token"]];
                 NSString *avatar = [dict[@"data"][@"headPhoto"] isEqual:[NSNull null]] ? @"":dict[@"data"][@"headPhoto"];
                 [meSelf setAvatarUrl:avatar];
             }];
             [SRMe setCurrentUser:me];
             [subject sendNext:me];
         }
         else
         {
             NSError * error = [NSError errorWithDomain:@"login_error"
                                                   code:[dict[@"code"] intValue]
                                               userInfo:@{@"tip":dict[@"tip"]}];
             [subject sendError:error];
         }
     }
                                  failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

//注册 3
+ (RACSignal *)registerWithUserName:(NSString *)userName
                            userPwd:(NSString *)userPwd
                           nickName:(NSString *)nickName
{
    RACSubject * subject = [RACSubject subject];
    [SRAccountNetSource registerWithUserName:userName
                                     userPwd:[userPwd md5]
                                    nickName:nickName
                                     success:^(NSDictionary *dict)
     {
         if ([dict[@"code"] intValue] == 0)
         {
             SRMe *me = [SRMe userWithName:userName password:userPwd];
             [me configAttributesWithBlock:^(SRMe *meSelf) {
                 meSelf.nickName = nickName;
                 meSelf.userPwd = userPwd;
                 meSelf.userName = userName;
                 meSelf.token = [NSString stringWithFormat:@"%@",dict[@"data"][@"token"]];
                 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                 [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
                 meSelf.createDate = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate date]]];
             }];
             [SRMe setCurrentUser:me];
             [subject sendNext:me];
         }
         else
         {
             [subject sendError:[NSError errorWithDomain:@"error"
                                                   code:[dict[@"code"] intValue]
                                               userInfo:@{@"tip":dict[@"tip"]}]];
         }
     }
                                     failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

//修改个人资料 6
+ (RACSignal *)updateProfileWithNickName:(NSString *)nickName
                                birthDay:(NSString *)birthDay
                                  weight:(NSNumber *)weight
                                  height:(NSNumber *)height
                                  gender:(NSNumber *)gender
{
    RACSubject * subject = [RACSubject subject];
    SRMe * me = [SRMe currentUser];
    [SRAccountNetSource updateProfileWithToken:me.token
                                      nickName:nickName
                                      birthDay:birthDay
                                        weight:weight
                                        height:height
                                        gender:gender
                                       success:^(NSDictionary *dict)
     {
         if ([dict[@"code"] intValue] == 0)
         {
             SRMe * me = [SRMe currentUser];
             [me configAttributesWithBlock:^(SRMe *meSelf) {
                 meSelf.nickName = nickName;
                 meSelf.birthday = birthDay;
                 meSelf.weight = weight;
                 meSelf.height = height;
                 meSelf.gender = gender;
             }];
             [subject sendNext:me];
         }
         else
         {
             [subject sendError:[NSError errorWithDomain:@"error"
                                                   code:[dict[@"code"] intValue]
                                               userInfo:@{@"tip":dict[@"tip"]}]];
         }

     }
                                       failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

//获取个人资料 7
+ (RACSignal *)getProfile
{
    RACSubject * subject = [RACSubject subject];
    SRMe * me = [SRMe currentUser];
    [SRAccountNetSource getProfileWithToken:me.token
                                    success:^(NSDictionary *dict)
     {
         if ([dict[@"code"] intValue] == 0)
         {
             SRMe * me = [SRMe currentUser];
             NSDictionary * data = dict[@"data"];
             [me configAttributesWithBlock:^(SRMe *meSelf) {
                 meSelf.nickName = DICT_VALUE(data, @"nickName");
                 NSString * headPhoto = DICT_VALUE(data, @"headPhoto");
                 if (headPhoto.length > 0) {
                     [meSelf setAvatarUrl:headPhoto];
                 }
                 meSelf.birthday = DICT_VALUE(data, @"birthDay");
                 id weight = DICT_VALUE(data, @"weight");
                 meSelf.weight = [weight length] > 0 ? @([weight intValue]) : @(0);
                 id height = DICT_VALUE(data, @"height");
                 meSelf.height = [height length] > 0 ? @([height intValue]) : @(0);
                 id gender = DICT_VALUE(data, @"gender");
                 meSelf.gender = [gender length] > 0 ? @([gender intValue]) : @(0);
                 meSelf.lastModifyDate = DICT_VALUE(data, @"lastModifyDate");
             }];
             [subject sendNext:me];
         }
         else
         {

            [subject sendError:[NSError errorWithDomain:@"error"
                                                code:[dict[@"code"] intValue]
                                            userInfo:@{@"tip":dict[@"tip"]}]];
         }

     }
                                    failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

//忘记密码 8
+ (RACSignal *)getAuthCodeWithPhoneNumber:(NSString *)phoneNumber
{
    RACSubject * subject = [RACSubject subject];
    [SRAccountNetSource getAuthCodeWithPhoneNumber:phoneNumber
                                           success:^(NSDictionary *dict)
     {
         if ([dict[@"code"] intValue] == 0)
         {
             [subject sendNext:nil];
         }
         else
         {
             [subject sendError:[NSError errorWithDomain:@"error"
                                                    code:[dict[@"code"] intValue]
                                                userInfo:@{@"tip":dict[@"tip"]}]];
         }
     }
                                           failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

//修改密码 9
+ (RACSignal *)modifyPasswordWithSms:(NSString *)sms
                              newPwd:(NSString *)newPwd
                         phoneNumber:(NSString *)phoneNumber
{
    RACSubject * subject = [RACSubject subject];
    [SRAccountNetSource modifyPasswordWithSms:sms
                                       newPwd:[newPwd md5]
                                  phoneNumber:phoneNumber
                                      success:^(NSDictionary *dict)
     {
         if ([dict[@"code"] intValue] == 0)
         {
             SRMe * me = [SRMe currentUser];
             [me configAttributesWithBlock:^(SRMe *meSelf) {
                 meSelf.userPwd = newPwd;
             }];
             [subject sendNext:me];
         }
         else
         {
             
             [subject sendError:[NSError errorWithDomain:@"error"
                                                    code:[dict[@"code"] intValue]
                                                userInfo:@{@"tip":dict[@"tip"]}]];
         }

     }
                                      failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

+ (RACSignal *)modifyUserHeadImg:(NSData *)imageData
{
    RACSubject * subject = [RACSubject subject];
    __block SRMe * me = [SRMe currentUser];
    [SRAccountNetSource modifyWithToken:me.token
                            userHeadImg:imageData
                                  success:^(NSDictionary *dict)
     {
         if ([dict[@"code"] intValue] == 0)
         {
             [me configAttributesWithBlock:^(SRMe *meSelf) {
                 NSString * avatarUrl = dict[@"data"][@"fileName"];
                 if (avatarUrl.length > 0) {
                     [meSelf setAvatarUrl:avatarUrl];
                 }
             }];
             [subject sendNext:me];
         }
     }
                                  failure:^(NSError *error)
     {
         [subject sendError:error];
     }];
    
    return subject;
}

@end
